//
//  DHWebViewController.m
//  精品酒店
//
//  Created by DIANGHONG on 2018/5/7.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "DHWebViewController.h"
#import <WebKit/WebKit.h>

@interface DHWebViewController ()<WKNavigationDelegate>

@property(nonatomic ,strong)WKWebView *webView;
@property(nonatomic ,strong)UIButton *popButton;
@property(nonatomic ,strong)NSString *popAppStrUrl;

@end

@implementation DHWebViewController

- (WKWebView *)webView{
    if (!_webView) {
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];

        _webView = [[WKWebView alloc] initWithFrame:self.view.bounds configuration:config];
    }
    return _webView;
}

- (void)setUrlStr:(NSString *)urlStr{
    _urlStr = urlStr;
    NSLog(@"%@",self.view.subviews.firstObject);
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    

    [self.view addSubview:self.webView];
    self.webView.navigationDelegate = self;
    self.webView.backgroundColor = UIColor.whiteColor;
    
}
#pragma mark: - WKNavigationDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    // 点击链接 进入
    if (navigationAction.navigationType == WKNavigationTypeLinkActivated)
    {
//        NSString *url = [NSString stringWithFormat:@"%@&comefrom=app",navigationAction.request.URL.absoluteString];
//        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
        NSString *url;
        if ([navigationAction.request.URL.absoluteString rangeOfString:@"?"].location == NSNotFound) {
            url = [NSString stringWithFormat:@"%@?comefrom=app",navigationAction.request.URL.absoluteString];
        }else{
            
            url = [NSString stringWithFormat:@"%@&comefrom=app",navigationAction.request.URL.absoluteString];
        }
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
       
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
   
    self.navigationController.navigationBar.hidden = false;
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:222.0f/255.0f green:60.0f/255.0f blue:50.0f/255.0f alpha:1];
    self.title = @"精品酒店网";
    [self.navigationController.navigationBar setTintColor:UIColor.whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:17],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
}
- (void)popButtonClick{
    [self.navigationController popViewControllerAnimated:true];
}
@end
